package zozopo.manual;

import java.sql.Connection;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;

import zozopo.database.BusiDatabaseManager;
import zozopo.manual.operations.ReleaseOS;
import zozopo.manual.operations.ReleaseTB;
import zozopo.operations.comm.DictionaryOS;
import zozopo.operations.comm.DictionaryTB;
import zozopo.user.operations.UserTB;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class PreModifyAction extends ActionSupport implements Action ,SessionAware,ServletRequestAware{
	
	private int id;
	private ReleaseTB release;
	private List lableList;
	
	public List getLableList() {
		return lableList;
	}

	public void setLableList(List lableList) {
		this.lableList = lableList;
	}

	public ReleaseTB getRelease() {
		return release;
	}

	public void setRelease(ReleaseTB release) {
		this.release = release;
	}


	private ReleaseOS releaseOS;
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
	HttpServletRequest request;

	private Map<Object, Object> session;
	
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}
	
	public String execute(){
		BusiDatabaseManager databaseConnection = new BusiDatabaseManager();
		Connection conn = databaseConnection.getConnection();
		
		releaseOS = new ReleaseOS(conn);
		release = releaseOS.getSubjectById(id);
		release.setRelease_context(release.getRelease_context().replaceAll("\r\n", ""));
		//release.setRelease_context(release.getRelease_context().replaceAll("<br>", ""));
		//request.setAttribute("message", release.getRelease_context());
		
		DictionaryOS dictionaryOS = new DictionaryOS(conn);
		lableList = dictionaryOS.getDictionaryLableList();
		
		databaseConnection.releaseConnection(conn);
		UserTB user = (UserTB) session.get("userInfo");
		
		if(release == null){
			return ERROR;
		}
		
		if(user == null){
			return NONE;
		}
		
		if(!user.getUser_email().equals(release.getRelease_useremail()) && ('0' == user.getUser_limitflag().charAt(1))){
			return NONE;
		}
		
		return SUCCESS;
	}
	

	public void validate() {
		// TODO Auto-generated method stub
		
		
	}
	
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		request = arg0;
		
	}
}
